﻿Imports Capa_de_Acceso_a_Datos
Public Class frmConsultarCuentasCorrientes

    Public FarmaciaId As Integer
    Private saldo As Double
    Private credito As Double
    Private debito As Double

    Private Sub frmConsultarCuentasCorrientes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        deshabilitarMovimientos()
    End Sub

    Private Sub deshabilitarMovimientos()
        gboMovimientos.Enabled = False
    End Sub

    Private Sub habilitarMovimientos()
        gboMovimientos.Enabled = True
    End Sub

    Private Sub btnConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsultar.Click
        habilitarMovimientos()
        dgvCuentaCorriente.DataSource = CuentaCorrienteDAO.obtenerCuentaCorrienteFarmacia(FarmaciaId, CDate(dtpDesde.Value), CDate(dtpHasta.Value))
        colorearGrilla()
        calcularSaldo()
    End Sub

    Private Sub colorearGrilla()
        With dgvCuentaCorriente
            ' alternar color de filas  
            .AlternatingRowsDefaultCellStyle.BackColor = Color.FloralWhite
            .DefaultCellStyle.BackColor = Color.Beige
        End With
    End Sub

    Private Sub calcularSaldo()
        Dim fila As DataGridViewRow
        saldo = 0
        credito = 0
        debito = 0
        For Each fila In dgvCuentaCorriente.Rows
            credito = credito + CDbl(fila.Cells("CuentaCorrienteMontoCredito").Value)
            debito = debito + CDbl(fila.Cells("CuentaCorrienteMontoDebito").Value)
        Next
        saldo = credito - debito
        txtSaldo.Text = "$" + Utilidades.darFormatoDouble(saldo)
    End Sub

    Private Sub btnSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalir.Click
        Me.Dispose()
    End Sub

    Private Sub btnLimpiar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLimpiar.Click
        dgvCuentaCorriente.DataSource = Nothing
        txtFarmacia.Text = ""
        txtSaldo.Text = ""
        FarmaciaId = Nothing
        dtpDesde.Value = Today
        dtpHasta.Value = Today
    End Sub

    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        frmSeleccionarFarmacia.frmLlamada = Me.Name
        frmSeleccionarFarmacia.ShowDialog()
    End Sub

End Class